package org.admin.controller;

import core.Data;
import core.data.DataResult;
import org.admin.entity.dto.UserDto;
import org.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    UserService service;
    // 创建更新
    @PostMapping("create")
    public DataResult create(@RequestBody Map<String, Object> post){
        return Data.create(post)
                .<UserDto>addHandle("params", data -> {
                    new UserDto().validate(new String[] {
                            "id", "email", "*username", "*password", "*nickname", "picture", "*gender", "*roleIds"
                    }).check(data);
                }).addHandle("create", service::create)
                .start().getResult();
    }
    // 用户列表
    @GetMapping("/query")
    public DataResult query(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<UserDto>addHandle("params", data -> {
                    new UserDto().validate(new String[]{
                            "nickname", "gender", "roleId", "*current", "size"
                    }).check(data);
                }).addHandle("query", service::query)
                .start().getResult();

    }
    @GetMapping("/fetch")
    public DataResult fetch(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<UserDto>addHandle("params", data -> {
                    new UserDto().validate(new String[]{
                            "*id"
                    }).check(data);
                }).addHandle("fetch", service::fetch)
                .start().getResult();

    }
    @PostMapping("/remove")
    // 删除 批量删除
    public DataResult remove(@RequestBody Map<String, Object> get){
        return Data.create(get)
                .<UserDto>addHandle("params", data -> {
                    new UserDto().validate(new String[] {
                            "*ids"
                    }).check(data);
                }).addHandle("remove", service::remove)
                .start().getResult();
    }
    /*



    // 获取用户详细信息
    public DataResult info(){


    }
    // 用户更新
    public DataResult update(){

    }

    // 用户更新密码
    public DataResult password(){


    }
     */
}
